package com.cloudcc.ccTool;

import java.util.ArrayList;
import java.util.List;

public class CCUtilities {
    //Get instance from INSTANCE.visual.force.com page so we can build
    public static String AutoAddNewLine(String str,Integer len){
        if(str == null) return null;
        String retstr = "";
        char[] destination = new char[str.length()]; // 创建一个与字符串长度相等的字符数组
        str.getChars(0, str.length(), destination, 0);


        Integer bytes = 0;
        Integer startChar = 0;
        Integer subStrLen = 0;
        Boolean HasChinese = false;
        for(char  c : destination){
            //By Vance 20230414 判断汉字的逻辑优化
            if( c >= 913 && c <= 65509){
                bytes += 2;
                HasChinese = true;
            }else{
                bytes += 1;
            }

            if(bytes > len && HasChinese){
                retstr += str.substring(startChar, subStrLen) + '\n';
                startChar = subStrLen;
                bytes = 0;
                HasChinese = false;
            }
            subStrLen ++;
        }
        if(startChar != 0){
            retstr += str.substring(startChar, subStrLen);
        }else{
            retstr = str;
        }
        return retstr;
    }
}
